<template>
  <div>
    <van-nav-bar title="注册页面" left-arrow @click-left="onClickLeft" />

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="用户名"
        :rules="[
          { required: true, message: '用户名不能为空' },
          { message: '请填写用户名', pattern: /^\w{5,}$/ }
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="pasword"
        placeholder="密码"
        :rules="[
          { required: true, message: '密码不能为空' },
          { message: '请填写密码', pattern: /^\w{6,}$/ }
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">注册</van-button>
      </div>
    </van-form>
    <router-link class="link" to="/register">注册完啦~去登录</router-link>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { register } from '@/api/user'
export default {
  name: 'Register',
  data () {
    return {
      username: '',
      password: ''
    }
  },

  methods: {
    async onSubmit (values) {
      await register(values)

      // 轻提示
      Toast('注册成功')
      // 使用编程式导航跳转登录页面
      this.$router.push('/login')
    },
    onClickLeft () {
      Toast('返回')
    }
  }
}
</script>

<style scoped lang="less">
.link {
  color: black;
  font-size: 12px;
  padding-left: 130px;
  font-size: 15px;
}
</style>
